home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / workbench / scanning / stscan / pixelop.c < prev    next >
C/C++ Source or Header  |  2000-03-05  |  7KB  |  275 lines

  1. /*
  2. **      $VER: pixelop.c 3.00D (11.8.97)
  3. **
  4. **      STScan pixel operation routines
  5. **
  6. **      Written by Frank-Christian Kruegel, Henning Peters, Andreas R. Kleinert
  7. **      GNU General Public License V2
  8. */
  9.  
  10. #include "stscan.h"
  11.  
  12. static ULONG histo[16];
  13.  
  14. void floyd(void)
  15. { signed short lbuf[2][4000];
  16.   UBYTE *grptr,*bwptr;
  17.   int i,j;
  18.   signed short f,g;
  19.   char wtitel[64];
  20.  
  21.  
  22.   g=(signed short)(winpar.threshold/4);
  23.   for (i=0;i<4000;i++) lbuf[0][i]=lbuf[1][i]=0;
  24.   grptr=bwptr=memptr;
  25.   for (i=0;i<memheight;i++)
  26.   { for (j=0;j<memwidth;j++)
  27.     { if (j&1) lbuf[0][j]=(signed short)(  (*(grptr+(j>>1)))     &15)+lbuf[1][j];
  28.       else     lbuf[0][j]=(signed short)(( (*(grptr+(j>>1))) >>4)&15)+lbuf[1][j];
  29.     }
  30.     for (j=0;j<(memwidth>>3);j++) *(bwptr+j)=0;
  31.     for (j=0;j<memwidth;j++)
  32.     { if (lbuf[0][j]<g)
  33.       { f=lbuf[0][j];
  34.         *(bwptr+(j>>3))|=bitval[j&7];
  35.       }
  36.       else f=lbuf[0][j]-15;
  37.       lbuf[1][j]=(3*f)/8;
  38.       lbuf[1][j+1]=f-2*lbuf[1][j];
  39.       lbuf[0][j+1]+=lbuf[1][j];     /* ((3*f)/8) */
  40.     }
  41.     grptr+=(memwidth>>1);
  42.     bwptr+=(memwidth>>3);
  43.     if (!(i%100))
  44.     { sprintf(&wtitel[0],"Dithern %3lf %%", (double) i*100/memheight);
  45.       SetWindowTitles(win,&wtitel[0],(UBYTE *)-1);
  46.     }
  47.   }
  48.   memneed=(memwidth*memheight)>>3;
  49.   memgray=0;
  50.   if (!(memptr=(UBYTE *)realloc(memptr,(memwidth*(memheight+ADDLIN))>>3)))
  51.   { memneed=0;
  52.     MessReq((UBYTE *)"Kein Speicher!");
  53.   }
  54.   SetWindowTitles(win," ",(UBYTE *)-1);
  55. }
  56.  
  57. void ordered(void)
  58. { UBYTE *grptr,*bwptr;
  59.   int i,j;
  60.   char wtitel[64];
  61.   UBYTE refmat[4][4]={{0,7,2,9},{11,4,13,6},{3,10,1,8},{14,7,12,5}};
  62.   UBYTE v;
  63.  
  64.   grptr=bwptr=memptr;
  65.   for (i=0;i<memheight;i++)
  66.   { for (j=0;j<memwidth;j++)
  67.     { v=(j&1)?(  (*grptr)     &15)
  68.              :(( (*grptr) >>4)&15);
  69.       if (v>refmat[j&3][i&3]) *bwptr&=invbitval[j&7];
  70.       else *bwptr|=bitval[j&7];
  71.       if ((j&7)==7) bwptr++;
  72.       if (j&1) grptr++;
  73.     }
  74.     if (!(i%100))
  75.     { sprintf(&wtitel[0],"Dithern %3lf %%", (double) i*100/memheight);
  76.       SetWindowTitles(win,&wtitel[0],(UBYTE *)-1);
  77.     }
  78.   }
  79.   memneed=(memwidth*memheight)>>3;
  80.   memgray=0;
  81.   if (!(memptr=(UBYTE *)realloc(memptr,(memwidth*(memheight+ADDLIN))>>3)))
  82.   { memneed=0;
  83.     MessReq((UBYTE *)"Kein Speicher!");
  84.   }
  85.   SetWindowTitles(win," ",(UBYTE *)-1);
  86. }
  87.  
  88. void thresh(void)
  89. { UBYTE *grptr,*bwptr;
  90.   int i,j;
  91.   char wtitel[64];
  92.   UBYTE v,g;
  93.  
  94.   grptr=bwptr=memptr;
  95.   g=winpar.threshold/4;
  96.   for (i=0;i<memheight;i++)
  97.   { for (j=0;j<memwidth;j++)
  98.     { v=(j&1)?(  (*grptr)     &15)
  99.              :(( (*grptr) >>4)&15);
  100.       if (v>g) *bwptr&=invbitval[j&7];
  101.       else *bwptr|=bitval[j&7];
  102.       if ((j&7)==7) bwptr++;
  103.       if (j&1) grptr++;
  104.     }
  105.     if (!(i%100))
  106.     { sprintf(&wtitel[0],"Binarisieren %3lf %%", (double) i*100/memheight);
  107.       SetWindowTitles(win,&wtitel[0],(UBYTE *)-1);
  108.     }
  109.   }
  110.   memneed=(memwidth*memheight)>>3;
  111.   memgray=0;
  112.   if (!(memptr=(UBYTE *)realloc(memptr,(memwidth*(memheight+ADDLIN))>>3)))
  113.   { memneed=0;
  114.     MessReq((UBYTE *)"Kein Speicher!");
  115.   }
  116.   SetWindowTitles(win," ",(UBYTE *)-1);
  117. }
  118.  
  119. void togray(void)
  120. { UBYTE *bwptr,*grptr;
  121.   int i,j;
  122.   char wtitel[64];
  123.   UBYTE v;
  124.   ULONG g;
  125.  
  126.   memneed=(memwidth*memheight)>>1;
  127.   if (!(memptr=(UBYTE *)realloc(memptr,(memwidth*(memheight+ADDLIN))>>1)))
  128.   { memneed=0;
  129.     MessReq((UBYTE *)"Kein Speicher!");
  130.   }
  131.   else
  132.   { memgray=1;
  133.     bwptr=memptr+((memwidth*memheight)>>3);
  134.     grptr=memptr+((memwidth*memheight)>>1);
  135.     for (i=0;i<memheight;i++)
  136.     { for (j=0;j<memwidth;j+=8)
  137.       { v= ~(*(--bwptr)); g=0;
  138.         if (v&128) g|=0xf0000000;
  139.         if (v& 64) g|=0x0f000000;
  140.         if (v& 32) g|=0x00f00000;
  141.         if (v& 16) g|=0x000f0000;
  142.         if (v&  8) g|=0x0000f000;
  143.         if (v&  4) g|=0x00000f00;
  144.         if (v&  2) g|=0x000000f0;
  145.         if (v&  1) g|=0x0000000f;
  146.         *(--grptr)=(UBYTE)(g&0xff);
  147.         *(--grptr)=(UBYTE)((g>>8)&0xff);
  148.         *(--grptr)=(UBYTE)((g>>16)&0xff);
  149.         *(--grptr)=(UBYTE)((g>>24)&0xff);
  150.       }
  151.       if (!(i%128))
  152.       { sprintf(&wtitel[0],"Umwandeln %3lf %%", (double) i*100/memheight);
  153.         SetWindowTitles(win,&wtitel[0],(UBYTE *)-1);
  154.       }
  155.     }
  156.     SetWindowTitles(win," ",(UBYTE *)-1);
  157.   }
  158. }
  159.  
  160.  
  161. void invert(void)
  162. { UBYTE *picptr;
  163.   ULONG i;
  164.  
  165.   picptr=memptr;
  166.   for (i=0;i<memneed;i++) *(picptr++)=~(*picptr);
  167. }
  168.  
  169. void mirrorh(void)
  170. { UBYTE *picptr;
  171.   ULONG i,j;
  172.   UBYTE temp[4000];
  173.   ULONG bpl;
  174.  
  175.   bpl=(memgray)?(memwidth>>1):(memwidth>>3);
  176.   picptr=memptr;
  177.   if (memgray)
  178.     for (i=0;i<memheight;i++)
  179.     { memcpy(&temp[1],picptr,bpl);
  180.       for (j=bpl;j>0;j--)
  181.         *(picptr++)=(((temp[j]&0x0f)<<4)|((temp[j]&0xf0)>>4));
  182.     }
  183.   else
  184.     for (i=0;i<memheight;i++)
  185.     { memcpy(&temp[1],picptr,bpl);
  186.       for (j=bpl;j>0;j--)
  187.         *(picptr++)=revbit[temp[j]];
  188.     }
  189. }
  190.  
  191. void mirrorv(void)
  192. { UBYTE *l1ptr,*l2ptr;
  193.   int i;
  194.   UBYTE temp[4000];
  195.   ULONG bpl;
  196.  
  197.   bpl=(memgray)?(memwidth>>1):(memwidth>>3);
  198.   l1ptr=memptr;
  199.   l2ptr=memptr+(memheight-1)*bpl;
  200.   for (i=0;i<(memheight>>1);i++)
  201.   { memcpy(&temp[0],l1ptr,bpl);
  202.     memcpy(l1ptr,l2ptr,bpl);
  203.     memcpy(l2ptr,&temp[0],bpl);
  204.     l1ptr+=bpl;
  205.     l2ptr-=bpl;
  206.   }
  207. }
  208.  
  209. static void counthisto(void)
  210. { ULONG cnt;
  211.   UBYTE *picptr;
  212.   UBYTE i,b;
  213.  
  214.   for (i=0;i<16;i++) histo[i]=0;
  215.   picptr=memptr;
  216.   for (cnt=0;cnt<memneed;cnt++)
  217.   { b=*(picptr++);
  218.     histo[b&0x0f]++;
  219.     histo[(b>>4)&0x0f]++;
  220.   }
  221. }
  222.  
  223. void viewhisto(struct RastPort *wrp)
  224. { ULONG max=0;
  225.   UBYTE i;
  226.   UWORD ampl;
  227.   UBYTE *plptr[4];
  228.  
  229.   counthisto();
  230.   for (i=0;i<16;i++) if (histo[i]>max) max=histo[i];
  231.   if (numcols==16)
  232.   { for (i=0;i<4;i++) plptr[i]=(UBYTE *)(rp->BitMap->Planes[i]+viewoffset);
  233.     clrscr16(plptr[0],plptr[1],plptr[2],plptr[3],viewheight,(screenwidth>>3)-2);
  234.   }
  235.   else
  236.   { for (i=0;i<2;i++) plptr[i]=(UBYTE *)(rp->BitMap->Planes[i]+viewoffset);
  237.     clrscr4(plptr[0],plptr[1],viewheight,(screenwidth>>3)-2);
  238.   }
  239.   SetWrMsk(wrp,15);
  240.   SetDrMd(wrp,JAM1);
  241.   SetDrPt(wrp,0xffff);
  242.   SetAPen(wrp,1);
  243.   Move(wrp,50,390); Draw(wrp,529,390);
  244.   for(i=0;i<16;i++)
  245.   { SetAPen(wrp,1);
  246.     ampl=histo[i]*350/max;
  247.     Move(wrp,50+i*30,390);
  248.     Draw(wrp,50+i*30,390-ampl);
  249.     Draw(wrp,79+i*30,390-ampl);
  250.     Draw(wrp,79+i*30,390);
  251.     if (ampl>1)
  252.     { if (numcols==16) SetAPen(wrp,(i+1)&0x0f);
  253.       else SetAPen(wrp,((i>>2)+1)&0x0f);
  254.       RectFill(wrp,51+i*30,391-ampl,78+i*30,389);
  255.     }
  256.   }
  257. }
  258.  
  259. void stretchhisto(void)
  260. { ULONG integ[16];
  261.   UBYTE lut[16];
  262.   UBYTE largelut[256];
  263.   UWORD i;
  264.   ULONG cnt;
  265.   UBYTE *picptr;
  266.  
  267.   counthisto();
  268.   integ[0]=histo[0];
  269.   for(i=1;i<16;i++) integ[i]=histo[i]+integ[i-1];
  270.   for(i=0;i<16;i++) lut[i]=(UBYTE)((integ[i]*15)/integ[15])&0x0f;
  271.   for(i=0;i<256;i++) largelut[i]=(((lut[(i>>4)&0x0f])<<4)|(lut[i&0x0f]));
  272.   picptr=memptr;
  273.   for(cnt=0;cnt<memneed;cnt++) {*picptr=largelut[*picptr]; picptr++; }
  274. }
  275.